package com.lmc.recreation;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.lmc.R;
import com.lmc.adapter.CrosstalkListAdapter;
import com.lmc.entity.RecreationList;
import com.lmc.util.JsonManager;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.Recreation;
import com.qxzn.app.page.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 娱乐——更多列表
 */
public class RecreationListActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {
    RecyclerView recyclerView;

    private List<RecreationList.DataBean.ItemsBean> list = new ArrayList<>();
    private int page;
    CrosstalkListAdapter adapter;

    private SmartRefreshLayout srl;

    private String name;
    private String id;


    @Override
    protected int initLayout() {
        return R.layout.activity_crosstalk_list;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        srl = findViewById(R.id.refreshLayout);
    }

    @Override
    protected void iniTitleBar() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        adapter = new CrosstalkListAdapter(RecreationListActivity.this, list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        name = getIntent().getStringExtra("name");
        id = getIntent().getStringExtra("id");
        tv_title.setText(name);

        srl.setOnRefreshListener(this);
        srl.setOnLoadmoreListener(this);

        Get(page = 1);
    }

    private void Get(int page) {
        JSONObject params = new JSONObject();
        params.put("media_id", id);
        HttpRequest.getService().getMediaList(HttpRequest.getDefaultBody(params)).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String string = response.body().string();
//                    Log.e("-----------", "获取娱乐模块列表：" + string);
                    RecreationList recreation = JsonManager.parseJson(string, RecreationList.class);
                    list.clear();
                    list.addAll(recreation.getData().getItems());
                    adapter.notifyDataSetChanged();

                } catch (Exception e) {
                    Log.e("-----", e.toString());
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });

    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh();
        Get(page = 1);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore();
        Get(++page);
    }
}
